package Player;

import java.util.Date;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(strict=false)
/**
 * 
 * @author Christopher Vogel
 *
 */
public class mPlayer implements Comparable<mPlayer> {
	@Element(name = "UserName")
	private String UserName;
	@Element(name = "Password")
	private String Password;
	@Element(name = "Points")
	private int Points = 0;
	@Element(name = "PlayedGames")
	private int PlayedGames = 0;
	@Attribute(name = "created")
	private long created;
	@Attribute(name = "modified")
	private long modified;

	public mPlayer(String UserName, String Password) {
		this.UserName = UserName;
		this.Password = this.MD5(Password);
		this.Points=0;
		this.PlayedGames=0;
		this.setCreated();
		this.setModified();
	}

	// Use only for deserialization
	protected mPlayer(@Element(name = "UserName") String UserName,
			@Element(name = "Password") String Password,
			@Element(name = "Points") int Points,
			@Element(name = "PlayedGames") int PlayedGames,
			@Attribute(name = "created") long created,
			@Attribute(name = "modified") long modified) {
		this.UserName = UserName;
		this.Password = Password;
		this.Points = Points;
		this.PlayedGames = PlayedGames;
		this.created = created;
		this.modified = modified;
	}

	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((Password == null) ? 0 : Password.hashCode());
		result = prime * result
				+ ((UserName == null) ? 0 : UserName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		mPlayer other = (mPlayer) obj;
		if (Password == null) {
			if (other.Password != null)
				return false;
		} else if (!Password.equals(other.Password))
			return false;
		if (UserName == null) {
			if (other.UserName != null)
				return false;
		} else if (!UserName.equals(other.UserName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return ""+UserName+"";
	}

	/* MD5 HashCode from http://stackoverflow.com/a/6565597 */
	public String MD5(String md5) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest
					.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}

	public String getUserName() {
		return UserName;
	}

	public void setUserName(String userName) {
		UserName = userName;
		this.setModified();
	}

	public String getPassword() {
		return Password;
	}

	public void setPassword(String password) {
		Password = this.MD5(password);
		this.setModified();
	}

	public int getPoints() {
		return Points;
	}

	public void addPoints(int points) {
		Points += points;
		this.setModified();
	}

	public int getPlayedGames() {
		return PlayedGames;
	}

	public void incrmentPlayedGames() {
		PlayedGames++;
		this.setModified();
	}

	public long getCreated() {
		return created;
	}

	private void setCreated() {
		Date Date = new Date();
		this.created = Date.getTime();
	}

	public long getModified() {
		return modified;
	}

	public void setModified() {
		Date Date = new Date();
		this.modified = Date.getTime();
	}

	@Override
	public int compareTo(mPlayer o) {
		if (this.equals(o))
			return 0;
		
		//Sortiert nach Punkte absteigend, danach sortiert nach Spiele aufsteigend
		if (this.Points > o.getPoints())
			return -1;
		else if (this.Points < o.getPoints())
			return 1;
		else {
			if (this.PlayedGames < o.getPlayedGames())
				return -1;
			else if (this.PlayedGames >= o.getPlayedGames())
				return 1;
		}
		return 1; //er motzt, darum hier.... kommt eh nie!
	}
}
